//
//  ByDocumentaryUserHeadView.m
//  caipiao_ios
//
//  Created by mc on 2018/8/17.
//  Copyright © 2018年 ZhangZhanbin. All rights reserved.
//

#import "ByDocumentaryUserHeadView.h"


@implementation ByDocumentaryUserHeadView

- (id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        
        UIImageView *backgroundView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        [self addSubview:backgroundView];
        backgroundView.userInteractionEnabled = YES;
        backgroundView.image = [UIImage imageNamed:@"zhuyebeijing背景"];
        
        UIImageView *iconView = [[UIImageView alloc] init];
        [backgroundView addSubview:iconView];
        [iconView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(backgroundView).offset(44);
            make.centerX.equalTo(backgroundView).offset(0);
            make.size.mas_equalTo(CGSizeMake(50, 50));
        }];
        iconView.layer.cornerRadius = 25;
        iconView.layer.masksToBounds = YES;
        iconView.userInteractionEnabled = YES;
        
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
        [iconView addGestureRecognizer:tap];
        
        
        UILabel *nameLabel = [[UILabel alloc] init];
        [backgroundView addSubview:nameLabel];
        [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(iconView.mas_bottom).offset(10);
            make.left.equalTo(backgroundView).offset(0);
            make.right.equalTo(backgroundView).offset(0);
            make.height.mas_equalTo(@18);
        }];
        nameLabel.textColor = [UIColor whiteColor];
        nameLabel.textAlignment = NSTextAlignmentCenter;
        nameLabel.font = FONT(14);
        
        ByFocusControl *focusControl = [[ByFocusControl alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 84, 83, 84, 28)];
        [backgroundView addSubview:focusControl];
        self.focusControl = focusControl;
        
        
        UILabel *focusLabel = [[UILabel alloc] init];
        [backgroundView addSubview:focusLabel];
        [focusLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(backgroundView).offset(0);
            make.top.equalTo(nameLabel.mas_bottom).offset(12);
            make.height.mas_equalTo(@20);
            make.width.mas_equalTo(@((SCREEN_WIDTH - 30) / 2.0));
        }];
        focusLabel.textColor = [UIColor whiteColor];
        focusLabel.font = FONT(14);
        focusLabel.tag = 100;
        focusLabel.userInteractionEnabled = YES;
        focusLabel.text = @"关注：";
        focusLabel.textAlignment = NSTextAlignmentRight;
        self.focusLabel = focusLabel;
        
        UILabel *fansLabel = [[UILabel alloc] init];
        [backgroundView addSubview:fansLabel];
        [fansLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(focusLabel.mas_top).offset(0);
            make.right.equalTo(backgroundView).offset(0);
            make.height.mas_equalTo(@20);
            make.width.mas_equalTo(@((SCREEN_WIDTH - 30) / 2.0));
        }];
        fansLabel.textColor = [UIColor whiteColor];
        fansLabel.font = FONT(14);
        fansLabel.tag = 200;
        fansLabel.userInteractionEnabled = YES;
        fansLabel.text = @"粉丝：";
        self.fansLabel = fansLabel;
        
        
        
        UIView *blackView = [[UIView alloc] init];
        [backgroundView addSubview:blackView];
        [blackView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(backgroundView).offset(0);
            make.right.equalTo(backgroundView).offset(0);
            make.bottom.equalTo(backgroundView).offset(0);
            make.height.mas_equalTo(@65);
        }];
        blackView.backgroundColor = rgba(0, 0, 0, 0.65);
        
        CGFloat width = SCREEN_WIDTH / 4.0;
        CGFloat height = 65;
        
        ByDocumentaryUserTitleValue *titleView1 = [[ByDocumentaryUserTitleValue alloc] initWithFrame:CGRectMake(0, 0, width, height)];
        [blackView addSubview:titleView1];
        titleView1.titleLabel.text = @"0人";
        titleView1.valueLabel.text = @"累计带红";
        
        ByDocumentaryUserTitleValue *titleView2 = [[ByDocumentaryUserTitleValue alloc] initWithFrame:CGRectMake(width, 0, width, height)];
        [blackView addSubview:titleView2];
        titleView2.titleLabel.text = @"0%";
        titleView2.valueLabel.text = @"盈利率";
        
        ByDocumentaryUserTitleValue *titleView3 = [[ByDocumentaryUserTitleValue alloc] initWithFrame:CGRectMake(width * 2, 0, width, height)];
        [blackView addSubview:titleView3];
        titleView3.titleLabel.text = @"0%";
        titleView3.valueLabel.text = @"命中率";
        
        ByDocumentaryUserTitleValue *titleView4 = [[ByDocumentaryUserTitleValue alloc] initWithFrame:CGRectMake(width * 3, 0, width, height)];
        [blackView addSubview:titleView4];
        titleView4.titleLabel.text = @"0元";
        titleView4.valueLabel.text = @"累计奖金";
        
        self.titleView1 = titleView1;
        self.titleView2 = titleView2;
        self.titleView3 = titleView3;
        self.titleView4 = titleView4;
        
        self.nameLabel = nameLabel;
        self.iconView = iconView;
        [focusControl addTarget:self action:@selector(controlAction:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return self;
}

- (void)setNetModel:(ByNetModel *)netModel{
    _netModel = netModel;
    
    self.titleView1.titleLabel.text = [NSString stringWithFormat:@"%@人",netModel.red_num];
    self.titleView2.titleLabel.text = [NSString stringWithFormat:@"%.f%%", netModel.earn_rate.floatValue];
    self.titleView3.titleLabel.text = @"0%";
    if (netModel.all_plan_num.integerValue > 0 && netModel.hit_number.integerValue > 0) {
        self.titleView3.titleLabel.text = [NSString stringWithFormat:@"%.f%%",netModel.hit_number.floatValue / netModel.all_plan_num.floatValue * 100];
    }
    self.titleView4.titleLabel.text = [NSString stringWithFormat:@"%@元", netModel.win_amount];
    [self.iconView sd_setImageWithURL:[NSURL URLWithString:netModel.head_pic] placeholderImage:HEAD_ICOM_IMAGE];
    self.nameLabel.text = netModel.nickname;
    
    self.focusLabel.text = [NSString stringWithFormat:@"关注：%@",netModel.follow_num];
    self.fansLabel.text = [NSString stringWithFormat:@"粉丝：%@",netModel.followed_num];
    if (![netModel.uid isEqualToString:BY_USER.uid]) {
        self.focusControl.hasFocused = netModel.is_follow.integerValue == 1;
    }
    
   
}


- (void)controlAction:(UIControl *)control{
    
    ByNetModel *netModel = [[ByNetModel alloc] init];
    [ByCoreToolAlert showLoadind:@"处理中"hasMasker:YES];
    netModel.uid = _netModel.uid;
    if (self.netModel.is_follow.integerValue == 1) {
        netModel.status = @"2";
    }else if (self.netModel.is_follow.integerValue == 0){
        netModel.status = @"1";
    }
    [ByDocumentaryApi followOrderFollow:netModel success:^(ByNetBaseResult *result) {
        
        [ByCoreToolAlert showSuccessWithText:@"操作成功"];
        if (self.updateBlock) {
            self.updateBlock();
        }
        if (self.netModel.is_follow.integerValue == 1) {
            self.netModel.is_follow = @"0";
        }else if (self.netModel.is_follow.integerValue == 0){
            self.netModel.is_follow = @"1";
        }
        self.focusControl.hasFocused = netModel.is_follow.integerValue == 1;
        
        
    } failure:^(ByNetBaseResult *result) {
        [ByCoreToolAlert showFailureWithText:result.msg];
    }];
    
}

- (void)tapAction:(UITapGestureRecognizer *)tap{
    
    if (self.tapViewBlock) {
        self.tapViewBlock();
    }
}

@end
